class ApplicationController < ActionController::Base
  protect_from_forgery
  
  before_filter :authorize, :except => :login
  before_filter :set_user
	
        def set_user 
          if session[:worker_id]
            @user    = Worker.find(session[:worker_id])
          else 
            @user    = nil 
          end
	end

	def get_all_workers 
	@workers = Worker.find(:all).sort
	end

	def get_all_grades
	@grades = Grade.find(:all).sort
	end

	def get_all_chains
	@chains = Chain.find(:all).sort
	end

	def get_all_colors
	@colors = Color.find(:all).sort
	end
 
  def create_comment_event(worker, route)
    event = Event.new(:data => "#{worker.to_link} hat einen Kommentar zu der Route " +
                               "#{route.to_link} von #{route.route_workers_to_link} hinzugef&uuml;gt")
    event.eventtypes << Eventtype.find_by_name("comments")
    event.save

    route.events << event
    route.save
  end

  def create_route_event(route)
    grammar = ""
    if(route.workers.size > 1)
      grammar = "haben"
    else
      grammar = "hat"
    end

    routeString = "#{route.color.name.downcase}e #{route.grade.grade}" 

    event = Event.new(:data => "#{route.route_workers_to_link} #{grammar} eine neue <a href=\"/routes/#{route.id}\">#{routeString}</a> im Wandteil #{route.chain.wall.to_link} geschraubt.")

    et = Eventtype.find_by_name("new_route")
    if et
      event.eventtypes << et     
      event.save
      route.events << event
      route.save
    end

  end

  def isSchorsch
    unless Worker.find_by_id(session[:worker_id]).is_schorsch?
      redirect_to "/"
    end
  end

  protected
  def authorize
    unless Worker.find_by_id(session[:worker_id])
      flash[:notice] = "Bitte melde dich an."
      redirect_to "/login/login" 
    end
  end
end
